package com.ruoyi.khpx.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.khpx.domain.Cxdw;
import com.ruoyi.khpx.domain.CxdwFj;
import com.ruoyi.khpx.mapper.CxdwMapper;
import com.ruoyi.khpx.service.ICxdwService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 创新单位Service业务层处理
 *
 * @author huacai
 * @date 2024-12-02
 */
@Service
public class CxdwServiceImpl implements ICxdwService {
    @Resource
    private CxdwMapper cxdwMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询创新单位
     *
     * @param cxdwId 创新单位主键
     * @return 创新单位
     */
    @Override
    public Cxdw selectCxdwByCxdwId(String cxdwId) {
        return cxdwMapper.selectCxdwByCxdwId(cxdwId);
    }

    /**
     * 查询创新单位列表
     *
     * @param cxdw 创新单位
     * @return 创新单位
     */
    @Override
    public List<Cxdw> selectCxdwList(Cxdw cxdw) {
        return cxdwMapper.selectCxdwList(cxdw);
    }

    /**
     * 新增创新单位
     *
     * @param cxdw 创新单位
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCxdw(Cxdw cxdw) {
        cxdw.setCreateTime(DateUtils.getNowDate());
        int rows = cxdwMapper.insertCxdw(cxdw);
        insertCxdwFj(cxdw);
        return rows;
    }

    /**
     * 批量新增创新单位
     *
     * @param cxdws 创新单位List
     * @return 结果
     */
    @Override
    public int batchInsertCxdw(List<Cxdw> cxdws) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(cxdws)) {
            try {
                for (int i = 0; i < cxdws.size(); i++) {
                    int row = cxdwMapper.insertCxdw(cxdws.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == cxdws.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改创新单位
     *
     * @param cxdw 创新单位
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCxdw(Cxdw cxdw) {
        cxdw.setUpdateTime(DateUtils.getNowDate());
//        cxdwMapper.deleteCxdwFjByCxdwId(cxdw.getCxdwId());
        insertCxdwFj(cxdw);
        return cxdwMapper.updateCxdw(cxdw);
    }

    /**
     * 批量删除创新单位
     *
     * @param cxdwIds 需要删除的创新单位主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCxdwByCxdwIds(String[] cxdwIds) {
        cxdwMapper.deleteCxdwFjByCxdwIds(cxdwIds);
        return cxdwMapper.deleteCxdwByCxdwIds(cxdwIds);
    }

    /**
     * 删除创新单位信息
     *
     * @param cxdwId 创新单位主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCxdwByCxdwId(String cxdwId) {
        cxdwMapper.deleteCxdwFjByCxdwId(cxdwId);
        return cxdwMapper.deleteCxdwByCxdwId(cxdwId);
    }

    /**
     * 新增附件
     * @param cxdwFj
     * @return
     */
    @Override
    public int insertCxdwFjByCxdwId(CxdwFj cxdwFj) {
        return cxdwMapper.insertCxdwFjByCxdwId(cxdwFj);
    }

    /**
     * 根据ID查询附件列表
     * @param cxdwFj
     * @return
     */
    @Override
    public List<CxdwFj> selectCxdwFjList(CxdwFj cxdwFj) {
        return cxdwMapper.selectCxdwFjList(cxdwFj);
    }

    /**
     * 删除创新单位附件
     * @param fjIds
     * @return
     */
    @Override
    public int deleteCxdwFjByFjIds(Integer[] fjIds) {
        return cxdwMapper.deleteCxdwFjByFjIds(fjIds);
    }

    /**
     * 新增创新单位附件信息
     *
     * @param cxdw 创新单位对象
     */
    public void insertCxdwFj(Cxdw cxdw) {
        List<CxdwFj> cxdwFjList = cxdw.getCxdwFjList();
        String cxdwId = cxdw.getCxdwId();
        if (StringUtils.isNotNull(cxdwFjList)) {
            List<CxdwFj> list = new ArrayList<CxdwFj>();
            for (CxdwFj cxdwFj : cxdwFjList) {
                cxdwFj.setCxdwId(cxdwId);
                list.add(cxdwFj);
            }
//            if (list.size() > 0) {
//                cxdwMapper.batchCxdwFj(list);
//            }
        }
    }
}
